package top.blueShark.template.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import top.blueShark.template.common.Result;
import top.blueShark.template.entity.dto.UserRolesDTO;
import top.blueShark.template.entity.SysRole;
import top.blueShark.template.service.SysRoleService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色相关接口
 */
@RestController
@RequestMapping("/api/system/role")
public class RoleController {

	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Resource
	private SysRoleService sysRoleService;


	@PostMapping("/names")
	public Map<String, Object> getRoleIdsList(@RequestBody UserRolesDTO userRolesDTO) {
		logger.info("获取角色列表 [{}] 的角色IDS",  userRolesDTO);
		List<String> ids = null;
		if (userRolesDTO.getUserRolesName() != null){
			ids = sysRoleService.getRoleIdsList(userRolesDTO.getUserRolesName());
		}
		Map<String, Object> data = new HashMap<>();
		data.put("ids", ids);

		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("data", data);
		result.put("error", null);
		result.put("message", "ok");
		return result;
	}


	/**
	 * TODO 获取角色列表 where status = 1
	 */
	/**
	 * 获取角色列表分页数据
	 */
	@GetMapping("/list/status")
	public Map<String, Object> getRoleListByStatus() {
		List<SysRole> items = sysRoleService.getRoleListByStatus("1");
		Map<String, Object> data = new HashMap<>();
		data.put("items", items);
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("data", data);
		result.put("error", null);
		result.put("message", "ok");
		return result;
	}

	/**
	 * 获取角色列表分页数据
	 */
	@GetMapping("/list")
	public Map<String, Object> getRoleList(@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int pageSize,
			@RequestParam(required = false) Integer status,
			@RequestParam(required = false) String name) {
		logger.info("获取角色列表，页码：{}，每页数量：{}", page, pageSize);
		List<SysRole> items = sysRoleService.getRoleList(page, pageSize, status, name);
		int total = sysRoleService.getRoleCount();

		Map<String, Object> data = new HashMap<>();
		data.put("items", items);
		data.put("total", total);

		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("data", data);
		result.put("error", null);
		result.put("message", "ok");
		return result;
	}


	@PostMapping
	public Map<String, Object> addRole(@RequestBody SysRole role) {
		logger.info("接收到添加角色请求，角色名称：{}", role.getName());
		sysRoleService.addRole(role);

		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("data", null);
		result.put("error", null);
		result.put("message", "添加成功");
		return result;
	}

	@PutMapping("/{id}")
	public Map<String, Object> updateRole(@PathVariable String id, @RequestBody SysRole role) {
		logger.info("接收到更新角色请求，角色ID：{}", id);
		role.setId(id); // 确保ID一致
		sysRoleService.updateRole(role);

		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("data", null);
		result.put("error", null);
		result.put("message", "更新成功");
		return result;
	}

	@DeleteMapping("/{id}")
	public Result deleteRole(@PathVariable String id) {
		logger.info("接收到删除角色请求，角色ID：{}", id);
		sysRoleService.deleteRoleById(id);
		return Result.success("删除成功");
	}
}